//
//  MCCountDownView.m
//  ZFGT
//
//  Created by apple on 2018/2/26.
//  Copyright © 2018年 allyoubank.com. All rights reserved.
//

#import "MCCountDownView.h"
@interface MCCountDownView ()

//临时变量
@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,strong)MCCountDownViewBlock temBlock;
@property(nonatomic,strong)MCVerificationTimeCountBlock temCountBlock;

@property(nonatomic,strong)dispatch_source_t _dispatch_source_t;

@end
@implementation MCCountDownView


- (UIButton *)btn
{
    if (_btn == nil) {
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        _btn.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height );
        [_btn setTitle:self.defaultTitle forState:UIControlStateNormal];
        _btn.titleLabel.font = self.font;
        _btn.backgroundColor = self.bgColor;
        [_btn setTitleColor:self.originTitleColor forState:UIControlStateNormal];
//        _btn.layer.cornerRadius = 3;
//        _btn.layer.masksToBounds = YES;
//        //_btn.layer.borderColor = FLB_orangeColor.CGColor;
//        _btn.layer.borderWidth = 1;
    }
    
    return _btn;
}

- (void)btnAction:(UIButton *)btn
{
    if (self.temBlock) {
        self.temBlock(self);
    }
    
    if ([self.delegate respondsToSelector:@selector(beginClickWithCodeView:)]) {
        [self.delegate beginClickWithCodeView:self];
    }
}

- (void)beginClickCodeView:(MCCountDownViewBlock)codeViewBlock countBlock:(MCVerificationTimeCountBlock)coutBlock
{
    self.temBlock = codeViewBlock;
    self.temCountBlock = coutBlock;
}

- (void)beginClickCodeView:(MCCountDownViewBlock)codeViewBlock
{
    self.temBlock = codeViewBlock;
}

- (void)cancleCountdown
{
    if (__dispatch_source_t != nil) {
        dispatch_source_cancel(__dispatch_source_t);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.btn.userInteractionEnabled = YES;
        });
    }
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpConfig];
    }
    return self;
}

- (void)setUpConfig
{
    self.defaultTitle = @"获取验证码";
    self.font = [UIFont systemFontOfSize:13];
    self.bgColor = [UIColor whiteColor];
    self.originTitleColor = [UIColor orangeColor];
    self.dynamicTitleColor = [UIColor orangeColor];
    self.maxTimeNum = 60;
    [self addSubview:self.btn];
}

// 开启倒计时效果
-(void)openCountdown
{
    NSTimeInterval seconds = self.maxTimeNum; //倒计时时间
    NSDate *endTime = [NSDate dateWithTimeIntervalSinceNow:seconds]; // 最后期限

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    __dispatch_source_t = _timer;
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        int interval = [endTime timeIntervalSinceNow];

        if(interval <= 0){ //倒计时结束，关闭
            if (self.temCountBlock) {
                self.temCountBlock(interval);
            }
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [self.btn setTitle:@"重新发送" forState:UIControlStateNormal];
                [self.btn setTitleColor:self.originTitleColor forState:UIControlStateNormal];
                self.btn.userInteractionEnabled = YES;
            });
            
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [self.btn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", interval] forState:UIControlStateNormal];
                [self.btn setTitleColor:self.dynamicTitleColor forState:UIControlStateNormal];
                self.btn.userInteractionEnabled = NO;
            });
            
            if (self.temCountBlock) {
                self.temCountBlock(interval);
            }
            
            if ([self.delegate respondsToSelector:@selector(beginClickWithTimeCount:)]) {
                [self.delegate beginClickWithTimeCount:interval];
            }
        }
    });
    dispatch_resume(_timer);
}

#pragma mark - 设置属性
- (void)setDefaultTitle:(NSString *)defaultTitle
{
    _defaultTitle = defaultTitle;
    [self.btn setTitle:defaultTitle forState:UIControlStateNormal];
   
}

- (void)setFont:(UIFont *)font
{
    _font = font;
    self.btn.titleLabel.font = font;
}

- (void)setBgColor:(UIColor *)bgColor
{
    _bgColor = bgColor;
    self.btn.backgroundColor = bgColor;
}

- (void)setOriginTitleColor:(UIColor *)originTitleColor
{
    _originTitleColor = originTitleColor;
    [self.btn setTitleColor:originTitleColor forState:UIControlStateNormal];
}


@end
